package com.ruoyi.web.messageDispose;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.websocket.CustomerClientService;
import com.ruoyi.web.websocket.CustomerMessageService;
import com.ruoyi.web.websocket.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.websocket.Session;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @author wwy
 * @date 2021-02-28 9:31
 */

public class CustomerMessageDispose {
    public static ApplicationContext applicationContext;

    private CustomerClientService customerClientService;

    private CustomerMessageService customerMessageService;
    /**
     * 消息处理
     * 分发给指定用户
     */
    public void messageDispose(Message message){
        customerClientService = applicationContext.getBean(CustomerClientService.class);
        customerMessageService = applicationContext.getBean(CustomerMessageService.class);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String destinationId = message.getDestinationId();
        message.setDateTime(simpleDateFormat.format(new Date()));
        Integer type = message.getType();
        if(StringUtils.isEmpty(destinationId)){
            //首次连接
            //判断是否为门户端
            if(type == 0){
                //是
                //寻找服务端进行建立通道
                this.createChannel(message);
            }
        }else{
            //不是首次连接，发送消息
            this.sendMessage(message);
        }
    }

    /**
     * 建立通道
     * @param message
     */
    public void createChannel(Message message){
        customerClientService = applicationContext.getBean(CustomerClientService.class);
        customerMessageService = applicationContext.getBean(CustomerMessageService.class);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map<String, Session> userClients = CustomerMessageService.userClients;
        String userId = null;
        for (Map.Entry<String, Session> stringSessionEntry : userClients.entrySet()) {
            String key = stringSessionEntry.getKey();
            userId = key;
            Session value = stringSessionEntry.getValue();
            break;
        }
        if(StringUtils.isEmpty(userId)){
            message.setDestinationId(userId);
            //发送消息
            Message sendMessage = new Message();
            sendMessage.setDestinationId(message.getUserId());
            sendMessage.setText("连接失败，请稍后重试！！！！");

            sendMessage.setDateTime(simpleDateFormat.format(new Date()));
            customerClientService.sendMessage(message.getUserId(), JSONObject.toJSONString(sendMessage));
        }else{
            message.setDestinationId(userId);
            //发送消息
            Message sendMessage = new Message();
            sendMessage.setDestinationId(message.getUserId());
            sendMessage.setUserId(userId);
            sendMessage.setText("你好，需要什么帮助吗！！！！");
            sendMessage.setDateTime(simpleDateFormat.format(new Date()));
            customerClientService.sendMessage(message.getUserId(), JSONObject.toJSONString(sendMessage));
        }

    }

    /**
     * 发送消息
     * @param message
     */
    public void sendMessage(Message message){
        customerClientService = applicationContext.getBean(CustomerClientService.class);
        customerMessageService = applicationContext.getBean(CustomerMessageService.class);
        Integer type = message.getType();
        String userId = message.getUserId();
        String destinationId = message.getDestinationId();
        if(type == 0){
            //门户=》服务端
            //获取服务端在线
            customerMessageService.sendMessage(destinationId,JSONObject.toJSONString(message));
        }else{
            //服务端=》门户
            customerClientService.sendMessage(destinationId, JSONObject.toJSONString(message));
        }
    }
}
